package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysDbConfig;
import com.ruoyi.system.service.ISysDbConfigService;

/**
 * 数据库配置 信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dbconfig")
public class SysDbConfigController extends BaseController
{
    @Autowired
    private ISysDbConfigService dbConfigService;

    /**
     * 获取数据库配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDbConfig dbConfig)
    {
        startPage();
        List<SysDbConfig> list = dbConfigService.selectDbConfigList(dbConfig);
        return getDataTable(list);
    }

    /**
     * 导出数据库配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:export')")
    @Log(title = "数据库配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDbConfig dbConfig)
    {
        List<SysDbConfig> list = dbConfigService.selectDbConfigList(dbConfig);
        ExcelUtil<SysDbConfig> util = new ExcelUtil<SysDbConfig>(SysDbConfig.class);
        return util.exportExcel(list, "数据库配置数据");
    }

    /**
     * 根据数据库配置编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:query')")
    @GetMapping(value = "/{dbId}")
    public AjaxResult getInfo(@PathVariable Long dbId)
    {
        return AjaxResult.success(dbConfigService.selectDbConfigById(dbId));
    }

    /**
     * 新增数据库配置
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:add')")
    @Log(title = "数据库配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDbConfig dbConfig)
    {
        if (!dbConfigService.checkDbNameUnique(dbConfig))
        {
            return AjaxResult.error("新增数据库配置'" + dbConfig.getDbName() + "'失败，数据库名称已存在");
        }
        dbConfig.setCreateBy(getUsername());
        return toAjax(dbConfigService.insertDbConfig(dbConfig));
    }

    /**
     * 修改数据库配置
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:edit')")
    @Log(title = "数据库配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDbConfig dbConfig)
    {
        if (!dbConfigService.checkDbNameUnique(dbConfig))
        {
            return AjaxResult.error("修改数据库配置'" + dbConfig.getDbName() + "'失败，数据库名称已存在");
        }
        dbConfig.setUpdateBy(getUsername());
        return toAjax(dbConfigService.updateDbConfig(dbConfig));
    }

    /**
     * 测试数据库连接
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:test')")
    @Log(title = "数据库配置", businessType = BusinessType.OTHER)
    @PostMapping("/test")
    public AjaxResult testConnection(@RequestBody SysDbConfig dbConfig)
    {
        boolean result = dbConfigService.testDbConnection(dbConfig);
        if (result)
        {
            return AjaxResult.success("数据库连接测试成功");
        }
        else
        {
            return AjaxResult.error("数据库连接测试失败，请检查配置信息");
        }
    }

    /**
     * 删除数据库配置
     */
    @PreAuthorize("@ss.hasPermi('system:dbconfig:remove')")
    @Log(title = "数据库配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dbIds}")
    public AjaxResult remove(@PathVariable Long[] dbIds)
    {
        return toAjax(dbConfigService.deleteDbConfigByIds(dbIds));
    }
} 